package yagwl.client.view.demo;

import yagwl.client.model.demo.LaureateRpcService;
import yagwl.client.model.demo.LaureateRpcServiceAsync;
import yagwl.client.widget.FormField;
import yagwl.client.widget.FormFieldGroup;
import yagwl.client.widget.FormPanel;
import yagwl.client.widget.InputTextArea;
import yagwl.client.widget.InputTextBox;
import yagwl.client.widget.InputWidgetContainer;
import yagwl.client.widget.Section;
import yagwl.client.widget.InputTextBox.DataType;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;

public class ContactCreateForm extends Composite {

  public final static String HistoryToken = "contactCreate";

  private final Section rootSP = new Section("Create Contact");
  private FormPanel rootFP = new FormPanel();
  private InputTextBox fnameTB = new InputTextBox(DataType.AlphaSpace, 100);
  private InputTextBox lnameTB = new InputTextBox(DataType.AlphaSpace, 100);
  private InputTextBox emailTB = new InputTextBox(DataType.Email, 100);
  private TextBox pwdTB = new PasswordTextBox();
  private TextBox pwd2TB = new PasswordTextBox();
  private TextBox hintTB = new TextBox();
  private TextBox homephTB = new TextBox();
  private TextBox workphTB = new TextBox();
  private TextBox cNameTB = new TextBox();
  private TextBox cPhoneTB = new TextBox();
  private InputTextArea descTA = new InputTextArea().setInitialLines(4)
      .setLineIncrements(1);
  private InputTextArea moreTA = new InputTextArea().setInitialLines(4)
      .setLineIncrements(2).setTextAreaWidth("200px").setMaxCharacters(120);

  public ContactCreateForm() {
    drawForm();
    HTML h = new HTML(
        "FormPanel and FormFieldGroup implement common patterns in forms. <ul>"
            + "<li>Field arrangement: Single or multiple columns; field spanning multiple columns; empty cells; horizontal or vertical tab flow."
            + "<li>Field group: Collapsible groups; vertical alignment across groups; header, description and footnote at form and group levels."
            + "<li>Button group: Add button; busy indicator; success or error messages."
            + "</ul>Input Widgets <ul>"
            + "<li>Displayed hint and popup hint for fields."
            + "<li>Elastic text area - grows as you type; limits max characters and displays counter for characters left."
            + "</ul>");
    rootSP.setDescription(h);
    rootSP.setContent(rootFP);
    initWidget(rootSP);
  }

  private void drawForm() {
    rootFP.setLabelColumnWidth("120px").setDataColumnWidth("100px");

    // identity
    FormFieldGroup idFGP = new FormFieldGroup("Personal Information")
        .setNumColumns(2);
    FormField emailFF = new FormField().setLabel("Email")
        .setDataWidget(emailTB).setSpanColumns(true);
    emailTB.setVisibleLength(30);
    InputWidgetContainer fnameIW = new InputWidgetContainer(fnameTB)
        .setDisplayedHint("Displayed Hint").setMouseOverHint("Mouseover Hint");
    fnameIW.renderUI();

    idFGP.addField("First Name", fnameIW);
    idFGP.addField("Last Name", lnameTB);
    idFGP.addField(emailFF);
    idFGP.addField(new FormField()); // empty
    idFGP.addField("Password", pwdTB);
    idFGP.addField("Re-enter Password", pwd2TB);
    idFGP.addField("Password Hint", hintTB);
    idFGP.addField(new FormField()); // empty
    idFGP.addField("Home Phone", homephTB, false);
    idFGP.addField("Work Phone", workphTB, false);
    idFGP.setDescription("This group has horizontal tab flow");
    rootFP.addFieldGroup(idFGP);

    // company
    FormFieldGroup companyFFG = new FormFieldGroup("Company Information", true);
    companyFFG.addField("Company Name", cNameTB);
    companyFFG.addField("Company Phone", cPhoneTB);
    InputWidgetContainer descIWC = new InputWidgetContainer(descTA)
        .setDisplayedHint("Elastic text area")
        .setMouseOverHint("Resizes as you type").renderUI();
    companyFFG.addField("Description", descIWC, false);
    InputWidgetContainer moreIWC = new InputWidgetContainer(moreTA)
        .setMouseOverHint("Uses a counter for max 120 characters").renderUI();
    companyFFG.addField("More Description", moreIWC, false);
    moreTA
        .setText("The best part of waking up is not Folgers in your cup, but knowing that Chuck Norris didn't kill you in your sleep.");
    companyFFG.setFootnote("This group is collapsible");
    rootFP.addFieldGroup(companyFFG);

    // addresses
    FormFieldGroup addrFGP = new FormFieldGroup("Addresses");
    addrFGP.setTabFlow(FormFieldGroup.Vertical);
    addrFGP.setDescription("This group has vertical tab flow");
    AddressFields shipAF = new AddressFields();
    AddressFields billAF = new AddressFields();
    addrFGP.addField(new FormField().setColumnHeader(true).setLabel(
        "Shipping Address"));
    addrFGP.addField(shipAF.getAddr1FF());
    addrFGP.addField(shipAF.getAddr2FF());
    addrFGP.addField(shipAF.getCityFF());
    addrFGP.addField(shipAF.getStateFF());
    addrFGP.addField(shipAF.getZipFF());
    shipAF.getZipFF().setLastFieldInColumn(true);
    addrFGP.addField(new FormField().setColumnHeader(true).setLabel(
        "Billing Address"));
    addrFGP.addField(billAF.getAddr1FF());
    addrFGP.addField(billAF.getAddr2FF());
    addrFGP.addField(billAF.getCityFF());
    addrFGP.addField(billAF.getStateFF());
    addrFGP.addField(billAF.getZipFF());
    rootFP.addFieldGroup(addrFGP);

    rootFP.addButton("Submit 1", new SubmitClickHandler());
    rootFP.addButton("Submit 2", new Submit2ClickHandler());
    rootFP.renderUI();
  }

  public class SubmitClickHandler implements ClickHandler {
    @Override
    public void onClick(ClickEvent event) {
      rootFP.setBusy(true);
      LaureateRpcServiceAsync demoService = GWT
          .create(LaureateRpcService.class);
      demoService.causeDelay(new AsyncCallback<String>() {

        @Override
        public void onFailure(Throwable caught) {
        }

        @Override
        public void onSuccess(String result) {
          rootFP.setMessage("Contact created (fake message)!");
        }
      });
    }
  }

  public class Submit2ClickHandler implements ClickHandler {
    @Override
    public void onClick(ClickEvent event) {
      rootFP.setBusy(true);
      LaureateRpcServiceAsync demoService = GWT
          .create(LaureateRpcService.class);
      demoService.causeDelay(new AsyncCallback<String>() {

        @Override
        public void onFailure(Throwable caught) {
        }

        @Override
        public void onSuccess(String result) {
          rootFP.setErrorMessage("Error (fake message)!");
        }
      });
    }
  }
}
